import json
import time
import datetime

from pip._vendor import requests
import json

from common import KLineEnum


class Students:
    # 日k
    # stock_exchange:深股为0. 上证为1. 开头
    # size:天数，即条数
    def day_k_size(stock_code, stock_name, size):
        print("获取日K,股票编码" + stock_code + '其他信息' + stock_name)
        time_stamp = int(round(time.time() * 1000))
        path_url = 'http://66.push2his.eastmoney.com/api/qt/stock/kline/get?cb=jQuery1124015253095575263576_'\
                   + str(time_stamp) \
                   + '&secid=' + ('1' if str.startswith(stock_code, '6') else '0') + '.' + str(stock_code) \
                   + '&ut=fa5fd1943c7b386f172d6893dbfba10b&fields1=f1%2Cf2%2Cf3%2Cf4%2Cf5' \
                   '&fields2=f51%2Cf52%2Cf53%2Cf54%2Cf55%2Cf56%2Cf57%2Cf58&klt=101' \
                   '&fqt=0&end=20500101&lmt=' + str(size) + '&_=' + str(time_stamp)
        # print(path_url)
        resp = requests.get(path_url)
        resp_content = str(resp.content.decode('utf-8'))
        print(resp_content)
        days_info_str = resp_content.split('(')[1].split(')')[0]
        resp_json = json.loads(days_info_str)
        klines = resp_json['data']['klines']
        # record_day_k_local(stock_code, klines)
        format_str = KLineEnum.Stock.format_k_line(klines[0])
        print(format_str)
        return klines

    def study(self):

        result = Students.day_k_size('601318', '中国平安', '1')
        print(result)
